package com.adobe.catalog.CustomComponent
{
	import Skin.ButtonBar.CustomButtonBarSkin;
	
	import flash.events.Event;
	
	import mx.core.IVisualElement;
	import mx.events.FlexEvent;
	
	import spark.components.ButtonBar;
	import spark.components.Group;
	import spark.layouts.HorizontalLayout;
	
	public class HorizontalButtonbar extends ButtonBar
	{
		[SkinPart(required="false")]
		public var maskGroup:Group;
		
		public function HorizontalButtonbar()
		{
			super();
			setStyle("skinClass",CustomButtonBarSkin);
			addEventListener(FlexEvent.VALUE_COMMIT,onValueCommitHandler );
			addEventListener(FlexEvent.CREATION_COMPLETE,onCompleteHandler );
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList( unscaledWidth,unscaledHeight );
			var n:int = dataGroup.numElements;
			var w:Number = 0;
			if( selectedIndex == -1 )
			{
				maskGroup.x = -100;
				return;
				
			}
			for( var i:int=0;i<n;i++ )
			{
				var child:IVisualElement = dataGroup.getElementAt(i);
				if( i != selectedIndex )
				{
					w+=(child.getPreferredBoundsWidth());	
				}
				else
				{
					w+=child.getPreferredBoundsWidth()/2;
					break;
				}
			}
			if( w == 0 )
				maskGroup.x = -100;
			else
				maskGroup.x = w-maskGroup.width/2;
		}
		
		private function onValueCommitHandler(event:Event):void
		{
			invalidateDisplayList();
		}
		
		/**
		 * 构造完毕后选中第一个button,让表示按钮Move到正确的位置 
		 * 
		 */		
		public function onCompleteHandler(event:Event=null):void
		{
			
			if( dataGroup.numElements != 0)
			{
				selectedIndex = 0;
			}
		}
	}
}